//
//  ZJStartAndEndTimeView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/22.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJStartAndEndTimeView.h"

@interface ZJStartAndEndTimeView ()
{
    NSInteger index;
}
/** 时间选择器 */
@property (nonatomic, strong) LPChooseOnlyHourAndMintueView *timeSeleView;
/** dateFormatter */
@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@end

@implementation ZJStartAndEndTimeView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame title:title];
    if (self) {
        [self addSubview:self.startTime];
        //至
        CGFloat zhiW = [NSString zj_sizeOfString:@"至" font:[UIFont titleTextFont] width:self.width].width;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.startTime.right + 11 * [UIScreen zj_screenWScale], self.titleLabel.bottom, zhiW, self.startTime.height)];
        label.text = @"至";
        label.textColor = [UIColor textBlackColor];
        label.font = [UIFont titleTextFont];
        [self addSubview:label];
        [self addSubview:self.endTime];
        self.dateFormatter = [NSDateFormatter new];
        self.dateFormatter.dateFormat = @"HH:mm";
    }
    return self;
}

#pragma mark - <ZJTimeSelectViewDelegate>协议方法
- (void)ZJTimeSelectViewDelegate_showTimeClick:(ZJTimeSelectView *)view{
    if (view == self.startTime) {
        index = 1;
        [self.timeSeleView show:self.startTime.time.text];
    }else{
        index = 2;
        [self.timeSeleView show:self.endTime.time.text];
    }
    
}


#pragma mark - LPCostomChooseYearAndMonthAndDayDelegate 协议方法
-(void)LPChooseOnlyHourAndMintueWithTitle:(NSString * )string;{
    
    if (index == 1) {
        self.startTime.time.text = string;
    }else{
        self.endTime.time.text = string;
        NSDate *endDate = [self.dateFormatter dateFromString:string];
        NSDate *stratDate = [self.dateFormatter dateFromString:self.startTime.time.text];
        NSInteger rulet = [endDate compare:stratDate];
        if (rulet == NSOrderedAscending) {//开始时间大
            self.endTime.time.text = self.startTime.time.text;
        }
    }
}
#pragma mark - Setter


#pragma mark - Getters
- (ZJTimeSelectView *)startTime{
    if (_startTime == nil){
         CGFloat width = (self.width - 2 * self.titleLabel.x - [NSString zj_sizeOfString:@"至" font:[UIFont titleTextFont] width:self.width].width - 22 * [UIScreen zj_screenWScale]) / 2;
        _startTime = [[ZJTimeSelectView alloc]initWithFrame:CGRectMake(self.titleLabel.x, self.titleLabel.bottom, width, 44 * [UIScreen zj_screenHScale])];
        _startTime.time.textAlignment = NSTextAlignmentCenter;
        _startTime.delegate = self;
        _startTime.isHideenImageView = YES;
    }
    return _startTime;
}

- (ZJTimeSelectView *)endTime{
    if (_endTime == nil){
        CGFloat zhiW = [NSString zj_sizeOfString:@"至" font:[UIFont titleTextFont] width:self.width].width + 22 * [UIScreen zj_screenWScale];
        _endTime = [[ZJTimeSelectView alloc]initWithFrame:CGRectMake(self.startTime.right + zhiW, self.titleLabel.bottom, self.startTime.width  , self.startTime.height)];
        _endTime.time.textAlignment = NSTextAlignmentCenter;
        _endTime.delegate = self;
        _endTime.isHideenImageView = YES;
    }
    return _endTime;
}

- (LPChooseOnlyHourAndMintueView *)timeSeleView{
    if (_timeSeleView == nil){
        _timeSeleView = [[LPChooseOnlyHourAndMintueView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight])];
        _timeSeleView.delegate = self;
    }
    return _timeSeleView;
}
@end
